package com.aat.dxfy.game.j46810.perfect;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyTankGame extends JFrame {
	private static final long serialVersionUID = 1L;
	MyPanel mp = null;

	public static void main(String[] args) {
		new MyTankGame();
	}

	public MyTankGame() {
		mp = new MyPanel();
		this.add(mp);// 把面板加入窗体
		// 注册监听
		this.addMouseListener(mp);
		this.addKeyListener(mp);
		this.addMouseMotionListener(mp);
		this.addWindowListener(mp);
		this.setTitle("坦克大战");// 窗体标题
		this.setSize(600, 400);// 大小，宽，高（像素）
		this.setLocation(300, 300);// 显示位置。左边距，上边距
		// 禁止用户改变窗口大小
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);// 显示
	}
}

// 我的面板
class MyPanel extends JPanel implements WindowListener, MouseListener, MouseMotionListener, KeyListener {// 定义一个我的坦克
	private static final long serialVersionUID = 1L;
	Hero hero = null;
	Diren diren = null;

	public MyPanel() {
		hero = new Hero(300, 200);
		diren = new Diren(100, 0);
		// diren.start();
	}

	public void paint(Graphics g) {
		super.paint(g);
		// 画坦克（到时候封装到一个函数）
		// 画左边矩形
		// g.setColor(Color.black);
		g.fillRect(0, 0, 600, 400);
		this.drawTank(hero.getX(), hero.getY(), g, Tank.dstct, 1);
		this.drawTank(diren.getX(), diren.getY(), g, 1, 0);
		this.drawTank(diren.getX() + 50, diren.getY(), g, 1, 0);
		this.drawTank(diren.getX() + 100, diren.getY(), g, 1, 0);

	}

	// 画坦克的函数
	public void drawTank(int x, int y, Graphics g, int direct, int type) {
		switch (type) {// 0我的坦克
		case 0:
			g.setColor(Color.blue);
			break;
		// 1敌人坦克
		case 1:
			g.setColor(Color.GREEN);
			break;

		}
		// 判断方向
		switch (direct) {// 0向上
		case 0:

			g.fill3DRect(x, y, 5, 30, false);// 左
			g.fill3DRect(x + 15, y, 5, 30, false);// 右
			g.fill3DRect(x + 5, y + 5, 10, 20, true);// 中
			g.setColor(Color.RED);
			g.drawLine(x + 10, y + 15, x + 10, y);// 线
			g.setColor(Color.YELLOW);
			g.fillOval(x + 5, y + 10, 10, 10);// 圆

			break;
		case 1:// 下
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x + 15, y, 5, 30, false);
			g.fill3DRect(x + 5, y + 5, 10, 20, true);
			g.setColor(Color.RED);
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			g.setColor(Color.YELLOW);
			g.fillOval(x + 5, y + 10, 10, 10);
			break;
		case 2:// 左
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, true);
			g.setColor(Color.RED);
			g.drawLine(x + 15, y + 10, x, y + 10);
			g.setColor(Color.YELLOW);
			g.fillOval(x + 10, y + 5, 10, 10);
			break;
		case 3:// 右
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, true);
			g.setColor(Color.RED);
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			g.setColor(Color.YELLOW);
			g.fillOval(x + 10, y + 5, 10, 10);
			break;

		}
	}

	public void keyPressed(KeyEvent e) {
		// 键按下监听
		System.out.println("按下：" + e.getKeyChar());
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			hero.setY(hero.getY() + hero.getSpeed());
			Tank.dstct = 1;
		} else if (e.getKeyCode() == KeyEvent.VK_UP) {
			hero.setY(hero.getY() - hero.getSpeed());
			Tank.dstct = 0;
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {

			hero.setX(hero.getX() - hero.getSpeed());
			Tank.dstct = 2;
		} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			hero.setX(hero.getX() + hero.getSpeed());
			Tank.dstct = 3;
		}
		this.repaint();
	}

	public void keyReleased(KeyEvent e) {
		// 键松开监听

	}

	public void keyTyped(KeyEvent e) {
		// 键的一个值被输出

	}

	public void mouseClicked(MouseEvent e) {
		// 点击
		System.out.println("鼠标被点击了x=" + e.getX() + "y=" + e.getY());
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		// 进入
		System.out.println("鼠标进入x=" + e.getX() + "y=" + e.getY());
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		// 离开
		System.out.println("鼠标离开x=" + e.getX() + "y=" + e.getY());
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		// 压下
		System.out.println("鼠标被按下x=" + e.getX() + "y=" + e.getY());
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标被松开x=" + e.getX() + "y=" + e.getY());
		// 松开

	}

	public void mouseDragged(MouseEvent e) {
		// 拖拽
		System.out.println("鼠标被拖拽x=" + e.getX() + "y=" + e.getY());
	}

	public void mouseMoved(MouseEvent e) {
		// 移动
		System.out.println("鼠标被移动x=" + e.getX() + "y=" + e.getY());
	}

	public void windowActivated(WindowEvent e) {
		// 激活TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent e) {
		// 窗口关闭

	}

	public void windowClosing(WindowEvent e) {
		// 正在关闭 TODO Auto-generated method stub

	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent e) {
		// 打开TODO Auto-generated method stub

	}
}

// 坦克父类
class Tank {// 坦克横坐标
	int x = 0;
	// 坦克纵坐标
	int y = 0;
	int speed = 5;// 速度
	static int dstct = 0;

	public Tank(int x, int y) {
		this.x = x;
		this.y = y;

	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public static int getDstct() {
		return dstct;
	}

	public static void setDstct(int dstct) {
		Tank.dstct = dstct;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

}

// 我的坦克
class Hero extends Tank {
	public Hero(int x, int y) {
		super(x, y);
	}
	// public int tankup
	// {
	// y=y-getSpeed();
	// }
	// public int tankdown
	// {
	// y=y+speed;
	// }
	// public int tankleft
	// {
	// x=x-speed;
	// }
	// public int tankright
	// {
	// x=x+speed;
	// }
	//
}

// 敌人坦克
class Diren extends Tank implements Runnable {
	public Diren(int x, int y) {
		super(x, y);
	}

	public void run() {
		while (true) { // 休眠一秒
						// 1000秒=1秒.进入堵塞。BLOCKED.
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}
	}
}
